/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package room;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import session.RestaurantsFacadeLocal;
import session.RoomsFacadeLocal;

/**
 *
 * @author TANGQUOCTUAN
 */
public class ControllerAdminRooms extends HttpServlet {
    @EJB
    private RestaurantsFacadeLocal restaurantsFacade;
    @EJB
    private RoomsFacadeLocal roomsFacade;
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            String action = request.getParameter("action");
            if (action.equals("AdminRooms")) {
                ServletContext context = getServletContext();
                ServletContext contextloadres = getServletContext();
                List room = this.roomsFacade.showRoom();
                context.setAttribute("listAdminRoom", room);
                List id = this.restaurantsFacade.getResID();
                contextloadres.setAttribute("resid", id);
                request.getRequestDispatcher("AdminRooms.jsp").forward(request, response);
            } else if (action.equals("Update")) {
                Integer id = new Integer(request.getParameter("id"));
                ServletContext context = getServletContext();
                List rooms = this.roomsFacade.detailRoom(id);
                context.setAttribute("updateRooms", rooms);
                request.getRequestDispatcher("UpdateRooms.jsp").forward(request, response);
            } else if (action.equals("UpdateRoom")) {
                ServletContext contextloadres = getServletContext();
                Integer id = new Integer(request.getParameter("txtid"));
                String name = request.getParameter("txtname").trim();
                Integer resid = new Integer(request.getParameter("txtids"));
                String des = request.getParameter("txtdes");
                boolean rs = this.roomsFacade.updateRoom(id, name, resid, des, "");
                List ids = this.restaurantsFacade.getResID();
                contextloadres.setAttribute("resid", ids);
                response.sendRedirect("ControllerAdminRooms?action=AdminRooms");
            } else if (action.equals("Delete")) {
                Integer id = new Integer(request.getParameter("id"));
                boolean rs = this.roomsFacade.deleteRoom(id);
                response.sendRedirect("ControllerAdminRooms?action=AdminRooms");
            } else if (action.equals("Insert")) {
                String name = request.getParameter("txtname");
                Integer id = new Integer(request.getParameter("txtid"));
                String des = request.getParameter("txtdes");
                this.roomsFacade.insertRoom(name, id, des, "1.jpg");
                response.sendRedirect("ControllerAdminRooms?action=AdminRooms");
            }
        } finally { 
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
